package com.zhu.day.aspects;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;

@Component
@Aspect
public class TimeSpeedAspect {

	private static final Logger log = LoggerFactory.getLogger(com.zhu.day.aspects.TimeSpeedAspect.class);

	/**
	 * 定义一个切入点. 解释下：
	 *
	 * ~ 第一个 * 代表任意修饰符及任意返回值. ~ 第二个 * 定义在web包或者子包 ~ 第三个 * 任意方法 ~ .. 匹配任意数量的参数.
	 */
	@Pointcut("execution(* com.zhu.day.controller.*.*(..))")
	public void timeSpeedPointcut() {
	}
	@org.aspectj.lang.annotation.Around("timeSpeedPointcut()")
	public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
		long start = System.currentTimeMillis();
		try {
			Object result = joinPoint.proceed();
			long end = System.currentTimeMillis();
			log.info("+++++ aspect " + joinPoint + "\tUse time : " + (end - start) + " ms!");
			return result;
		} catch (Throwable e) {
			long end = System.currentTimeMillis();
			log.info("+++++ aspect " + joinPoint + "\tUse time : " + (end - start) + " ms with exception : "
					+ e.getMessage());
			e.printStackTrace();
			throw e;
		}
	}

}
